package com.xh.bussiness.practice.controller;

import com.xh.bussiness.practice.bean.dto.SubmitPracticeResultDto;
import com.xh.bussiness.practice.bean.request.CorrectPracticeStudentRequest;
import com.xh.bussiness.practice.bean.request.PracticeRequest;
import com.xh.bussiness.practice.model.PracticeStudentResult;
import com.xh.bussiness.practice.service.IPracticeStudentResultService;
import com.xh.bussiness.practice.service.IPracticeStudentService;
import com.xh.bussiness.practice.model.PracticeStudent;
import com.xh.bussiness.practice.bean.request.PracticeStudentRequest;
import com.xh.core.bean.XhExportHeaderBean;
import com.xh.core.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ReactiveTypeDescriptor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import org.springframework.validation.annotation.Validated;

import com.xh.core.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * 学生作业表 前端控制器
 * </p>
 *
 * @author Pitt
 * @since 2021-01-26
 */
@RestController
@RequestMapping("/admin/practiceStudent")
public class PracticeStudentController extends BaseController {

    @Autowired
    private IPracticeStudentService practiceStudentService;
    @Autowired
    private IPracticeStudentResultService practiceStudentResultService;

    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @RequestMapping("/queryPage")
    public AjaxResult queryPage(XhPage page, PracticeStudentRequest bean){
        page=practiceStudentService.queryPracticeStudentPage(page,bean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, page);
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @RequestMapping("/query")
    public AjaxResult query(PracticeStudentRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",practiceStudentService.queryPracticeStudent(bean));
    }

    /**
     * 导出全部
     * @param bean
     * @return
     */
    @RequestMapping(value="/exportAll")
    public AjaxResult exportAll(PracticeStudentRequest bean, HttpServletRequest request) {
        try {
            LinkedHashMap<String,XhExportHeaderBean> exportHeaderBeans=new LinkedHashMap<>();
            //exportHeaderBeans.put("username",new XhExportHeaderBean("用户名"));
            //exportHeaderBeans.put("deleted",new XhExportHeaderBean("是cao否删除",ImmutableMap.of(0,"否",1,"是")));
            //exportHeaderBeans.put("createTime",new XhExportHeaderBean("创建时间","yyyy-MM-dd HH:mm:ss"));

            String downloadUrl=practiceStudentService.exportAll(request,exportHeaderBeans,bean,"queryPracticeStudentPage");
            if(downloadUrl!=null){
                return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",downloadUrl);
            }else{
                return new AjaxResult(AjaxResultConstant.Result.ERROR,"导出失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR);
        }
    }

    /**
     * 新增和保存
     * @param practiceStudent 实体对象
     * @return
     */
    @RequestMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Validated PracticeStudent practiceStudent){
        if(practiceStudent.getId()==null){
            practiceStudentService.insert(practiceStudent);
        }else{
            practiceStudentService.update(practiceStudent);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @RequestMapping("/deleteById/{id}")
    @Transactional
    public AjaxResult deleteById(@PathVariable String id){
        practiceStudentService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @RequestMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        practiceStudentService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 学生端作业列表
     * @param bean
     * @return
     */
    @RequestMapping("/queryPracticeStudentPageByStudentId")
    public AjaxResult queryPracticeStudentPageByStudentId(@RequestBody PracticeStudentRequest bean) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, practiceStudentService.queryPracticeStudentPageByStudentId(bean));
    }

    /**
     * 学生提交作答结果
     *
     * @return
     */
    @RequestMapping("/submitPracticeStudentResult")
    public AjaxResult submitPracticeStudentResult(@RequestBody SubmitPracticeResultDto practiceResultDto) {
        String checkedMsg = practiceResultDto.checkVal();
        if (!StringUtils.isEmpty(checkedMsg)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, checkedMsg);
        }
        practiceStudentService.submitPracticeStudentResult(practiceResultDto);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "提交成功");
    }

    /**
     * 查询学生作业详情
     * @param studentId
     * @param practiceId
     * @return
     */
    @RequestMapping("/queryPracticeStudentDetail")
    public AjaxResult queryPracticeStudentDetail(String studentId, String practiceId) {
        if (StringUtils.isEmpty(studentId) || StringUtils.isEmpty(practiceId)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, practiceStudentService.queryPracticeStudentDetail(studentId, practiceId));
    }

    /**
     * 查询学生作业
     * @return
     */
    @RequestMapping("/queryPracticeStudent")
    public AjaxResult queryPracticeStudent(PracticeStudentRequest request) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,practiceStudentService.queryPracticeStudent(request));
    }

    /**
     * 教师批改作业
     * @return
     */
    @RequestMapping("/modifyCorrectPracticeStudent")
    public AjaxResult modifyCorrectPracticeStudent(@RequestBody CorrectPracticeStudentRequest bean) {
        if (StringUtils.isEmpty(bean.getStudentPracticeId()) || CollectionUtils.isEmpty(bean.getStudentResults())) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        practiceStudentService.modifyCorrectPracticeStudent(bean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "批改成功");
    }


    /**
     * 查询学生作业报告
     * @param practiceId
     * @param studentId
     * @return
     */
    @RequestMapping("/queryPracticeStudentReport")
    public AjaxResult queryPracticeStudentReport(String practiceId, String studentId) {
        if (StringUtils.isEmpty(practiceId) || StringUtils.isEmpty(studentId)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, practiceStudentService.queryPracticeStudentReport(practiceId, studentId));
    }


    /**
     * 查询学生作答结果
     * @param practiceId
     * @param studentId
     * @return
     */
    @RequestMapping("/queryPracticeStudentResult")
    public AjaxResult queryPracticeStudentResult(String practiceId, String studentId) {
        if (StringUtils.isEmpty(practiceId) || StringUtils.isEmpty(studentId)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, practiceStudentService.queryPracticeStudentResult(practiceId, studentId));

    }

    /**
     * 教师评价学生普通作业
     * @param practiceId
     * @param studentId
     * @return
     */
    @RequestMapping("/commentStudentPractice")
    public AjaxResult commentStudentPractice(String practiceId, String studentId, double socre, String comment) {
        if (StringUtils.isEmpty(practiceId) || StringUtils.isEmpty(studentId)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, practiceStudentService.commentStudentPractice(practiceId, studentId, socre, comment));

    }

    /**
     * 教师评价学生普通作业
     * @param practiceId
     * @param studentId
     * @return
     */
    @RequestMapping("/queryPracticeDetails")
    public AjaxResult queryPracticeDetails(String practiceId, String studentId) {
        if (StringUtils.isEmpty(practiceId) || StringUtils.isEmpty(studentId)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, practiceStudentService.queryPracticeDetails( practiceId, studentId));

    }

    @RequestMapping("/updatePracticeStudentResult")
    public AjaxResult updatePracticeStudentResult(@RequestBody PracticeStudentResult result) {
        return new AjaxResult(practiceStudentResultService.update(result));
    }

    /**
     * 高频错题个数
     * @author 赵文博
     * @date 2021/4/27 16:24
     * @param classId
     * @return com.xh.core.bean.AjaxResult
     **/
    @GetMapping("/queryHighFrequencyErrorCount")
    public AjaxResult queryHighFrequencyErrorQuestionCount(String classId){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,
                                practiceStudentResultService.queryHighFrequencyErrorQuestionCount(classId));
    }

    /**
     * 高频错题列表
     * @author 赵文博
     * @date 2021/4/27 16:43
     * @param classId basicTypes
     * @return com.xh.core.bean.AjaxResult
     **/
    @GetMapping("/queryHighFrequencyErrorList")
    public AjaxResult queryHighFrequencyErrorQuestions(String classId,String basicType){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,
                practiceStudentResultService.queryHighFrequencyErrorQuestions(classId,basicType));
    }
}

